package com.access.boot.autoconfigure.apisdk.aop;

import com.access.boot.autoconfigure.apisdk.proxy.ProxyContainer;
import org.springframework.util.ClassUtils;

/**
 * @author Yangzhen
 * @date 2022/6/4 01:53
 * @since 1.0.0
 */
public class DefaultProxyFactory extends AdvisedSupport {

    public DefaultProxyFactory(Object target) {
        setTarget(target);
        setInterfaces(ClassUtils.getAllInterfaces(target));
    }

    public <T> T getProxy() {
        return (T) createAopProxy(this);
    }

    private synchronized <T> T createAopProxy(AdvisedSupport config) {
        T proxy = (T) new JdkDynamicAopProxy(config).getProxy();
        ProxyContainer.add(this.getTarget(), proxy);
        return proxy;
    }
}
